<?php
function game_go(&$arrReturn)
{
    global $db;
    $speNo = $_GET['no'];
    $isToAuto = true;
    $arrReturn['log'] .= LogFormat('begin time:' . date('Y-m-d H:i:s'));
    if(date( "H" ) < 9)
    {
    	//取下期时间
    	$sql = "SELECT id,kgtime FROM game28 WHERE kj = 0 AND kgtime > NOW() order by id limit 1";
        $query = $db->query($sql);
        if($rs = $db->fetch_array( $query ))
        {
             $arrRet['nextid'] = $rs['id'];
             $arrRet['nexttime'] = $rs['kgtime'];
        }
        else
        {
			 $sql = "select DATE_ADD(NOW(),INTERVAL 60 SECOND) nowtime";
        	 $query = $db->query($sql);
        	 $rs = $db->fetch_array( $query );
			 $arrRet['nextid'] ='0';
             $arrRet['nexttime'] = date('Y-m-d H:i:s',strtotime($rs["nowtime"]));
        }
    	$arrReturn['msg'] = 'not in open result time(bj)';
    	return;
	}
    
    $No = intval($speNo);
    if($speNo == 0) //取最新开奖
    {
		$sql = "select gameno from game_result where gametype = 'gamebj' and isopen = 0 order by gameno desc limit 1";
    	$query = $db->query($sql);
    	if($rs = $db->fetch_array( $query ))
    	{
    		$No = $rs["gameno"];
		}
    	else
    	{
    		//保证采集不到但下盘时间快到时自动下注
		    $sql = "SELECT id,kgtime,now() as nowtime FROM game28 WHERE kj = 0 AND zdtz_r = 0 AND kgtime > NOW() ORDER BY kgtime LIMIT 1";
		    $query = $db->query($sql);
		    if($rs = $db->fetch_array( $query ))
		    {
		        $NextNo = $rs['id'];
		        if(strtotime($rs['kgtime']) - strtotime($rs['nowtime']) < 30)
		        {
			        //自动投注
			        AutoPress($arrReturn,$No-1,$NextNo);
				}
		    }
    		$arrReturn['code'] = "ok";
    		$arrReturn['msg'] = "no open game record(bj)";
    		$arrReturn['log'] .= LogFormat("no open game record(bj)");
    		return;
		}
    }
    else
    {
    	$isToAuto = false;
		$sql = "SELECT id FROM game28 WHERE kj = 0 and id = '{$No}' ";
    	$query = $db->query($sql);
    	if($rs = $db->fetch_array( $query ))
    	{
    		$No = $rs["id"];
		}
    	else
    	{ 
    		$arrReturn['msg'] = "{$No} had opened(pk)";
    		$arrReturn['log'] .= LogFormat("{$No} had opened(pk)");
    		return;
		}
    }
    $arrReturn['no'] = $No;
    $strkjNum = GetGameResult($No);
    $kjnum_array = explode( "|", $strkjNum );
	
	if ( count($kjnum_array) == 20 ) //取到了
	{
		//排序
		sort($kjnum_array,SORT_NUMERIC);
		//更新开奖状态
	    $sql = "update game_result set isopen = 1,opentime = now() where gametype = 'gamebj' and gameno = '{$No}'";
	    $db->query($sql);
	    
		$arrReturn['log'] .= LogFormat("open result string:" . $strkjNum . " (bj)");
		$arrReturn['log'] .= LogFormat('begin open time:' . date('Y-m-d H:i:s'));
	    //开奖蛋蛋28
	    $zjh_a = $kjnum_array[0] + $kjnum_array[1] + $kjnum_array[2] + $kjnum_array[3] + $kjnum_array[4] + $kjnum_array[5];
	    $zjh_b = $kjnum_array[6] + $kjnum_array[7] + $kjnum_array[8] + $kjnum_array[9] + $kjnum_array[10] + $kjnum_array[11];
	    $zjh_c = $kjnum_array[12] + $kjnum_array[13] + $kjnum_array[14] + $kjnum_array[15] + $kjnum_array[16] + $kjnum_array[17];
	    $zj_a = substr( $zjh_a, -1 );
	    $zj_b = substr( $zjh_b, -1 );
	    $zj_c = substr( $zjh_c, -1 );
	    $zj_result = $zj_a + $zj_b + $zj_c;
		/* wk   */
 
		// $res = wk_get_kj_game28($No,$zj_a,$zj_b,$zj_c,$zj_result,$strkjNum);
	    $sql = "call web_kj_game28({$No},{$zj_a},{$zj_b},{$zj_c},{$zj_result},'{$strkjNum}')";
		$arr = $db->Mysqli_Multi_Query($sql);
		$time = time();
  
		$arrRet['code'] = $arr[0][0]["result"];
		$arrRet['msg'] = $arr[0][0]["msg"];
		$strlog = "game28 " . $No . " result is:{$arrRet['msg']}({$arrRet['code']})".  " (bj)";
		$arrReturn['log'] .= LogFormat($strlog);
		
		
		
		
		
		
		
		
		//开奖蛋蛋36
		/*$zjh_a = $kjnum_array[0] + $kjnum_array[1] + $kjnum_array[2] + $kjnum_array[3] + $kjnum_array[4] + $kjnum_array[5];
	    $zjh_b = $kjnum_array[6] + $kjnum_array[7] + $kjnum_array[8] + $kjnum_array[9] + $kjnum_array[10] + $kjnum_array[11];
	    $zjh_c = $kjnum_array[12] + $kjnum_array[13] + $kjnum_array[14] + $kjnum_array[15] + $kjnum_array[16] + $kjnum_array[17];
	    $zj_a = substr( $zjh_a, -1 );
	    $zj_b = substr( $zjh_b, -1 );
	    $zj_c = substr( $zjh_c, -1 );
	    $zj_result = GetGame36Result($zj_a,$zj_b,$zj_c);
	    $sql = "call web_kj_game36({$No},{$zj_a},{$zj_b},{$zj_c},{$zj_result},'{$strkjNum}')";
		$arr = $db->Mysqli_Multi_Query($sql);
		$arrRet['code'] = $arr[0][0]["result"];
		$arrRet['msg'] = $arr[0][0]["msg"];
		$strlog = "game36 " . $No . " result is:{$arrRet['msg']}({$arrRet['code']})".  " (bj)";
		$arrReturn['log'] .= LogFormat($strlog);*/
		//开奖北京28
		/*$zjh_a = $kjnum_array[1] + $kjnum_array[4] + $kjnum_array[7] + $kjnum_array[10] + $kjnum_array[13] + $kjnum_array[16];
	    $zjh_b = $kjnum_array[2] + $kjnum_array[5] + $kjnum_array[8] + $kjnum_array[11] + $kjnum_array[14] + $kjnum_array[17];
	    $zjh_c = $kjnum_array[3] + $kjnum_array[6] + $kjnum_array[9] + $kjnum_array[12] + $kjnum_array[15] + $kjnum_array[18];
	    $zj_a = substr( $zjh_a, -1 );
	    $zj_b = substr( $zjh_b, -1 );
	    $zj_c = substr( $zjh_c, -1 );
	    $zj_result = $zj_a + $zj_b + $zj_c;
	    $sql = "call web_kj_gameself28({$No},{$zj_a},{$zj_b},{$zj_c},{$zj_result},'{$strkjNum}')";
		$arr = $db->Mysqli_Multi_Query($sql);
		$arrRet['code'] = $arr[0][0]["result"];
		$arrRet['msg'] = $arr[0][0]["msg"];
		$strlog = "gameself28 " . $No . " result is:{$arrRet['msg']}({$arrRet['code']})".  " (bj)";
		$arrReturn['log'] .= LogFormat($strlog);*/
		
		//开奖北京36
		/*$zjh_a = $kjnum_array[1] + $kjnum_array[4] + $kjnum_array[7] + $kjnum_array[10] + $kjnum_array[13] + $kjnum_array[16];
	    $zjh_b = $kjnum_array[2] + $kjnum_array[5] + $kjnum_array[8] + $kjnum_array[11] + $kjnum_array[14] + $kjnum_array[17];
	    $zjh_c = $kjnum_array[3] + $kjnum_array[6] + $kjnum_array[9] + $kjnum_array[12] + $kjnum_array[15] + $kjnum_array[18];
	    $zj_a = substr( $zjh_a, -1 );
	    $zj_b = substr( $zjh_b, -1 );
	    $zj_c = substr( $zjh_c, -1 );
	    $zj_result = GetGame36Result($zj_a,$zj_b,$zj_c);
	    $sql = "call web_kj_gamebj36({$No},{$zj_a},{$zj_b},{$zj_c},{$zj_result},'{$strkjNum}')";
		$arr = $db->Mysqli_Multi_Query($sql);
		$arrRet['code'] = $arr[0][0]["result"];
		$arrRet['msg'] = $arr[0][0]["msg"];
		$strlog = "gamebj36 " . $No . " result is:{$arrRet['msg']}({$arrRet['code']})".  " (bj)";
		$arrReturn['log'] .= LogFormat($strlog);*/
		
		//开奖北京16
		/*$zjh_a = $kjnum_array[0] + $kjnum_array[3] + $kjnum_array[6] + $kjnum_array[9] + $kjnum_array[12] + $kjnum_array[15];
		$zj_a = ($zjh_a % 6) + 1;
	    $zjh_b = $kjnum_array[1] + $kjnum_array[4] + $kjnum_array[7] + $kjnum_array[10] + $kjnum_array[13] + $kjnum_array[16];
	    $zj_b = ($zjh_b % 6) + 1;
	    $zjh_c = $kjnum_array[2] + $kjnum_array[5] + $kjnum_array[8] + $kjnum_array[11] + $kjnum_array[14] + $kjnum_array[17];
	    $zj_c = ($zjh_c % 6) + 1;
	    $zj_result = $zj_a + $zj_b + $zj_c;  
	    $sql = "call web_kj_gamebj16({$No},{$zj_a},{$zj_b},{$zj_c},{$zj_result},'{$strkjNum}')";
		$arr = $db->Mysqli_Multi_Query($sql);
		$arrRet['code'] = $arr[0][0]["result"];
		$arrRet['msg'] = $arr[0][0]["msg"];
		$strlog = "gamebj16 " . $No . " result is:{$arrRet['msg']}({$arrRet['code']})".  " (bj)";
		$arrReturn['log'] .= LogFormat($strlog);
		$arrReturn['log'] .= LogFormat('end open time:' . date('Y-m-d H:i:s') . " (bj)");   */
	}
	else //取不到结果
	{
		$arrReturn['code'] = 'noreward';
		$arrReturn['msg'] = "no open {$No} result(bj)";
		$arrReturn['log'] = LogFormat("no open {$No} result!(bj)");
		return;
	}
	if($isToAuto)
	{
	    //给最新一盘自动投注
	    $NextNo = $No + 1;
	    $randScond = rand(3,5);
	    $sql = "SELECT id,DATE_ADD(NOW(),INTERVAL {$randScond} SECOND) nowtime FROM game28 WHERE kj = 0 and id={$NextNo} limit 1";
	    $query = $db->query($sql);
	    if($rs = $db->fetch_array( $query ))
	    {
	        $arrReturn['nextid'] = $rs['id'];
	        $arrReturn['nexttime'] = $rs['nowtime'];
	        //自动投注
	        AutoPress($arrReturn,$No,$NextNo);
	    }
	}
    
    
}
function AutoPress(&$arrReturn,$No,$NextNo)
{
	global $db;
	//蛋蛋28自动投注
	$sql = "call web_tz_game28_auto_new({$No},{$NextNo})"; 
	$arrT = $db->Mysqli_Multi_Query($sql);
	$strlog = "game28 " . $arrRet['nextid'] . " auto press(" .$arrT[0][0]["result"] . ")" . " (bj)";
	$arrReturn['log'] .= LogFormat($strlog);
	//蛋蛋36自动投注
	$sql = "call web_tz_game36_auto_new({$No},{$NextNo})";
	$arrT = $db->Mysqli_Multi_Query($sql);
	$strlog = "game36 " . $arrRet['nextid'] . " auto press(" .$arrT[0][0]["result"] . ")" . " (bj)";
	$arrReturn['log'] .= LogFormat($strlog);
	//北京28自动投注
	$sql = "call web_tz_gameself28_auto_new({$No},{$NextNo})";
	$arrT = $db->Mysqli_Multi_Query($sql);
	$strlog = "gameself28 " . $arrRet['nextid'] . " auto press(" .$arrT[0][0]["result"] . ")" . " (bj)";
	$arrReturn['log'] .= LogFormat($strlog);
	//北京36自动投注
	$sql = "call web_tz_gamebj36_auto_new({$No},{$NextNo})";
	$arrT = $db->Mysqli_Multi_Query($sql);
	$strlog = "game36 " . $arrRet['nextid'] . " auto press(" .$arrT[0][0]["result"] . ")" . " (bj)";
	$arrReturn['log'] .= LogFormat($strlog);
	//北京16自动投注
	$sql = "call web_tz_gamebj16_auto_new({$No},{$NextNo})";
	$arrT = $db->Mysqli_Multi_Query($sql);
	$strlog = "gamebj16 " . $arrRet['nextid'] . " auto press(" .$arrT[0][0]["result"] . ")" . " (bj)";
	$arrReturn['log'] .= LogFormat($strlog);
	$arrReturn['log'] .= LogFormat('end autopress time:' . date('Y-m-d H:i:s') . " (bj)");
}
function GetGame36Result($a,$b,$c)
{
	$arrNum = array($a,$b,$c);
	sort($arrNum);
	if($arrNum[0] == $arrNum[2]) //豹子
		return 1;
	if($arrNum[0] == $arrNum[1] || $arrNum[1] == $arrNum[2]) //对子
		return 2;
	if($arrNum[0] == 0 && ($arrNum[1] == 1 || $arrNum[1]==8) && $arrNum[2] == 9)//顺子特例
		return 3;
	if($arrNum[1] - $arrNum[0] == 1 && $arrNum[2] - $arrNum[1] == 1) //顺子
		return 3;
	if($arrNum[0] == 0  && $arrNum[2] == 9)//半顺特例
		return 4;
	if($arrNum[1] - $arrNum[0] == 1 || $arrNum[2] - $arrNum[1] == 1) //半顺
		return 4;
	return 5; //杂
}
function GetGameResult($theNo)
{ 
	global $db;
    $ret = "";
    
    $sql = "select gameresult from game_result where gametype='gamebj' and gameno = '{$theNo}' and isopen = 0"; 
    $query = $db->query($sql);
    if($rs = $db->fetch_array( $query ))
    {
    	$ret = $rs["gameresult"];
	}
	
	return $ret;
}



error_reporting( 0 );
set_time_limit(200);
include_once( "inc/conn.php" );
include_once( "inc/function.php" );
include_once( "inc/kj28.php" );
 
if ( $_GET['key'] == "kj_beijingtype_result" )
{
    $arrRet = array('code'=>'ok', //
    				'no' =>'',
                    'msg'=>'', //错误信息 -1:不在开奖时间内，0：开奖成功，1：该期已开过奖，2：取中奖赔率错误，99：系统错误
                    'log'=>'',
                    'nextid'=>'', //下一盘id
                    'nexttime'=>'' //下一篇开奖时间
        );
    game_go($arrRet);
    if ($arrRet['nextid'] == '')
    {
    	$RandSecond = rand(3,5);
		$sql = "select date_add(NOW(),INTERVAL {$RandSecond} SECOND) nowtime,id from game28 where kgtime > now() and kj = 0 order by id limit 1";
		$query = $db->query($sql);
		if($rs = $db->fetch_array( $query ))
		{
			$arrRet['nextid'] = $rs['id'];
			$arrRet['nexttime'] = date('Y-m-d H:i:s',strtotime($rs["nowtime"]));
		}
		else
		{
			$arrRet['nextid'] ='0';
            $arrRet['nexttime'] = date('Y-m-d H:i:s',strtotime("+{$RandSecond} second"));
		}
    }
    //返回
    echo json_encode($arrRet);
}

$db->close( );
